/*
    Project:        Home Power Control
    File:           HPCHumTemp.h
    Author:         christian@karunamusic.at
    Date:           12.2014
    Description:    Sensor to measure humidity and temperature with the DHT22.
                    Code partly from Adafruit
                    https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/software-install-updated
 */

#ifndef HPCHUMTEMP_H
#define HPCHUMTEMP_H

#include <stdint.h>
#include <HPCThreadedSensor.h>

/** \brief Baseclass of the DHT22-sensor to measure the humidity and temperature.
 */
class HPCHumTemp : public HPCThreadedSensor
{
public:
    /** \brief Constructor
     *
     * \param id database id of the sensor.
     * \param pin Inputpin.
     * \param interval time (in seconds) to pause between querying the sensor.
     *
     */
    HPCHumTemp( unsigned long id, unsigned int pin, unsigned int interval );
    /** \brief Destructor.
     */
    virtual ~HPCHumTemp();

protected:
    /** \brief Get a value from the sensor.
     */
    virtual void Measure();

    HPCValue m_Hum; /**< humidity */
    HPCValue m_Temp; /**< temperature */

    /** \brief Query the sensor and calculate the result.
     * Set m_Value and m_AddVal with the result.
     *
     * \return bool, true if successful.
     *
     */
    bool querySensor();
    /** \brief Sleep for given milliseconds.
     *
     * \param millis time to sleep.
     *
     */
    void sleep_milliseconds( uint32_t millis );
    /** \brief Waste time (and CPU) for given milliseconds.
     *
     * \param millis time to stay busy.
     *
     */
    void busy_wait_milliseconds( uint32_t millis );
    /** \brief Set highest priority for timecritical measurements.
     */
    void set_max_priority();
    /** \brief Set the default priority.
     */
    void set_default_priority();
};

/** \brief Class to measure the humidity of the DHT22-sensor.
 */
class HPCHumidity : public HPCHumTemp
{
public:
    /** \brief Constructor
     *
     * \param id database id of the sensor.
     * \param pin Inputpin.
     * \param interval time (in seconds) to pause between querying the sensor.
     *
     */
    HPCHumidity( unsigned long id, unsigned int pin, unsigned int interval )
        :HPCHumTemp(id,pin,interval) {}
    /** \brief Destructor.
     */
    virtual ~HPCHumidity();

    /** \brief Get the type of the sensor.
     *
     * \return SensorType Type of the sensor (from enum SensorType).
     *
     */
    virtual inline SensorType Type() const
      { return SensorType::HPCHumidity; }

protected:
    /** \brief Get a value from the sensor.
     */
    virtual void Measure();

    /** \brief Check if sensor has changed (called after interval pause).
     *
     * \return True if changed.
     */
    virtual bool hasChanged();
};

/** \brief Class to measure the temperature of the DHT22-sensor.
 */
class HPCTemperature : public HPCHumTemp
{
public:
    /** \brief Constructor
     *
     * \param id database id of the sensor.
     * \param pin Inputpin.
     * \param interval time (in seconds) to pause between querying the sensor.
     *
     */
    HPCTemperature( unsigned long id, unsigned int pin, unsigned int interval )
        :HPCHumTemp(id,pin,interval) {}
    /** \brief Destructor.
     */
    virtual ~HPCTemperature();

    /** \brief Get the type of the sensor.
     *
     * \return SensorType Type of the sensor (from enum SensorType).
     *
     */
    virtual inline SensorType Type() const
      { return SensorType::HPCTemperature; }

protected:
    /** \brief Get a value from the sensor.
     */
    virtual void Measure();

    /** \brief Check if sensor has changed (called after interval pause).
     *
     * \return True if changed.
     */
    virtual bool hasChanged();
};

#endif // HPCHUMTEMP_H
